home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
graphic
/
globe103.zip
/
GLOBE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-07
|
23KB
|
813 lines
/* globe - display 3D vectors partly hidden by a sphere
history...
6 Oct 90 1.03: ported to Turbo C. Calling scr_ci rather than
getchar. Frame around display is off by default.
'+' and '-' change amount by which scale changes.
4 Nov 88 1.02: added -ll option for longitude-latitude input
17 Jun 88 1.01: improved help page, fixed index bug, general
code cleanup.
1 Jun 88 Adapted from DOTS
bugs...
correct the hidden line elimination to account for these cases...
o---\-------/---o -> o---\ /---o
\_____/ \_____/
o---\---o / -> o---\ /
\_____/ \_____/
GLOBE should have the same flexible memory handling as GRAPH.
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "g3.h"
#include "g.h"
#include "scr_ci.h"
#define VERSION "1.03"
#define RIGHT "Copyright (c) 1985, 1990 by James R. Van Zandt. All rights reserved.\n"
#define RESTRICTION "This program may be reproduced for personal, non-profit use only.\n"
#define PI 3.14159265358979
#define d2r (PI/180.)
#define r2d (180./PI)
#define ENTRIES 3000
#define MAXLAB 200
#define MAXSTYLES 50
#define BUFSIZE 200
extern double best_width, best_height;
double radius=1.; /* sphere radius */
double radius2=.999; /* square of sphere radius, minus a bit */
extern int debugging;
int latlong = 0;
char string[80];
static char buf[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE];
char *default_script_file = "";
char *program_name = NULL;
int style[MAXSTYLES]; /* array of requested line styles */
int repeat[MAXSTYLES]; /* array of repeat counts for line styles */
int persp=0, /* nonzero if perspective projection desired */
markers=0, /* nonzero if marker desired at each data point */
framing=0; /* nonzero if frame desired around plot */
int tails=0;
int observer_viewpoint=0;
int scaling=0; /* nonzero if scaling data to fill a cube */
int breaking=0; /* nonzero if breaking (disconnecting) graph
after each label in input */
int automatic_abscissas=0; /* nonzero if abscissas are to be generated */
int abscissa_arguments=0; /* number specified: abscissa spacing & start */
int logx=0; /* nonzero if x axis is to be logrithmic */
int logy=0; /* nonzero if y axis is to be logrithmic */
int logz=0; /* nonzero if z axis is to be logrithmic */
int x_arguments=0; /* number specified: xmin, xmax, xdel */
int y_arguments=0; /* number specified: ymin, ymax, ydel */
int standard_input=0; /* nonzero if data is coming from standard input */
int labels=0; /* number of user-supplied labels
(index into next two arrays) */
int numstyles=0; /* # linestyles specified by user */
int p_data[MAXLAB]; char *p_text[MAXLAB];
char null_label[]="";
static double cc, ss, angle, step, factor=1.,
enlarge=1., /* scale factor for display size */
growth=1.414, /* factor for changing scale factor */
abscissa=0., /* default starting value for automatic abscissas */
abscissa_step=1., /* default step for automatic abscissas */
p1=-3., p2=0., p3=0., /* observer location */
ox, oy, /* screen coordinates of top left corner */
diag, /* extent of viewable volume */
data_size; /* extent of data */
double rotate[3][3];
#define REAL float
REAL *x, *y, *z;
double xmin, xmax, ymin, ymax, zmin, zmax;
int last;
main(argc, argv) int argc; char **argv;
{ int i, j;
if(argc<2 || (argc>1 && streq(argv[1], "?"))) sample();
read_data(argc, argv);
initialize_core(0,0,3);
select_view_surface(1);
initialize_view_surface(1);
initialize_parameters(argc, argv);
while(1)
{if(!plotting_device)
new_frame(); /* clears the screen */
if(persp) perspective(p1, p2, p3);
else parallel(p1, p2, p3);
view_plane_normal(-p1, -p2, -p3);
create_temporary_segment(1); /* finds viewing transformation */
frame(); /* draw framework around image */
find_transformation(p1, p2, p3);
draw_sphere();
#ifdef VIS
{
double u;
FILE *dfile;
dfile=fopen("err","a");
#define TV(a,b,c) fprintf(dfile, "visible(%f,%f,%f)=%d\n", a, b, c, visible(a,b,c))
for (u=-2.2; u<3.; u+=.25) {TV(0.,0.,u);}
for (u=-2.2; u<3.; u+=.25) {TV(0.,u,0.);}
for (u=-2.2; u<3.; u+=.25) {TV(u,0.,0.);}
fclose(dfile);
new_frame(); /* clears the screen */
}
#endif
image(); /* use drawing primitives to create image */
close_temporary_segment();
if(plotting_device) quit();
update_parameters();
}
}
initialize_parameters(argc, argv) int argc; char **argv;
{/* if(want("use right handed coordinate system? ")) */
coordinate_system_type(1); /* default coordinates are LH */
ndc_space_2(best_width, best_height); /* enable use of all of screen */
viewport2(0., best_width, 0., best_height); /* viewport is all of screen */
/* if(want("enable clipping? ")) */
clip_window(1);
view_up_3(0., 0., 1.); /* vector in this direction appears
vertical in final view */
view_reference_point((xmax+xmin)/2., (ymin+ymax)/2., (zmin+zmax)/2.);
window(-diag*best_width, diag*best_width,
-diag*best_height, diag*best_height); /* use all of view surface */
angle=3.14159/12.;
ss=sin(angle); cc=cos(angle);
}
update_parameters()
{ int c;
static double t;
while(1)
{c=scr_ci();
switch(c)
{case '+': angle *= 4.; step *= 4.; factor *= 4.;
growth *= growth;
growth *= growth;
ss=sin(angle); cc=cos(angle);
break;
case '-': angle /= 4.; step /= 4.; factor /= 4.;
growth = sqrt(sqrt(growth));
ss=sin(angle); cc=cos(angle);
break;
case '?': help(); return;
case 'q':
case 'Q': quit();
default:
{c &= 0xff;
if((c==up_char)|(c==('E'-64))) {p3+=step; return;}
else if((c==down_char)|(c==('X'-64))) {p3-=step; return;}
else if((c==right_char)|(c==('D'-64)))
{t=cc*p1-ss*p2; p2= ss*p1+cc*p2; p1=t; return;
}
else if((c==left_char)|(c==('S'-64)))
{t=cc*p1+ss*p2; p2=-ss*p1+cc*p2; p1=t; return;
}
else if(c == pgup_char)
{if(!persp) goto BIGGER;
p1/=growth; p2/=growth; p3/=growth; step/=growth; return;
}
else if(c == pgdn_char)
{if(!persp) goto SMALLER;
p1*=growth; p2*=growth; p3*=growth; step*=growth; return;
}
else if(c == home_char)
{
BIGGER: enlarge *= growth;
diag = data_size/enlarge;
window(-diag*best_width, diag*best_width,
-diag*best_height, diag*best_height);
ox=-diag*best_width; oy=diag*best_height*.95;
return;
}
else if(c == end_char)
{
SMALLER: enlarge /= growth;
diag = data_size/enlarge;
window(-diag*best_width, diag*best_width,
-diag*best_height, diag*best_height);
ox=-diag*best_width; oy=diag*best_height*.95;
return;
}
{
FILE *dfile;
dfile = fopen("debug","a");
fprintf(dfile, "illegal character %d = %02xH\n", c, c);
fclose(dfile);
}
putchar(7); /* beep */
break;
}
}
puts("\008 \008"); /* erase that character */
}
}
quit()
{
terminate_view_surface(1);
printf("last viewpoint was (%f, %f, %f),\n", p1, p2, p3);
printf("scale factor %f\n", enlarge);
exit(0);
}
char *msg[]={
" --- Status ---",
"",
buf,
buf2,
buf3,
"",
"",
" --- Key Commands ---",
"",
"cursor keys move observer along a cylinder: higher, ",
" lower, around to left, or around to right.",
" pg up move closer",
" pg dn move back",
" home enlarge",
" end shrink",
" + enlarge cursor key steps by a factor of 4",
" - shrink cursor key steps by a factor of 4",
" Q exit program",
"",
" ? print this help menu",
"",
"",
" <press any key to continue>",
0
};
help()
{ char **sp, c;
double x0, y0, z0, dz;
x0 = (xmax+xmin)/2.-diag*best_width;
z0 = (zmax+zmin)/2.+diag*best_height*.7;
dz = diag/24.;
new_frame();
parallel(0., -1.,